package com.ljn.controller;

import com.ljn.core.dto.common.Response;
import com.ljn.core.dto.common.User;
import com.ljn.core.dto.common.command.usercommand.*;
import com.ljn.core.dto.common.constant.ConstSession;
import com.ljn.facade.UserFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;


/**
 * Created by liujunnan on 2017/11/2.
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private final String DEFAULT_HEAD_IMAGE = "3779668271de4601a0dd54d77f045948.png";
    @Inject
    UserFacade userFacade;

    @ResponseBody
    @PostMapping(value = "/register")
    public Response register(@RequestBody Registercommand registercommand) {
        return userFacade.register(registercommand);
    }

    @ResponseBody
    @PostMapping(value = "/login")
    public Response login(@RequestBody LoginCommand logincommand, HttpSession session) {
        Response response = userFacade.login(logincommand);
        User user = (User) response.getData();
        if (null == user) {
            response.setCode(1);
            response.setMsg("登录失败");
            response.setData(null);
            return response;
        } else { //登陆成功之后将用户设置到session中
            session.setAttribute(ConstSession.LOGIN_USER_SESSION, user);
            Response result = userFacade.queryUserDetail(user);
            session.setAttribute(ConstSession.USER_DETAILL, result.getData()); //将用户的详细信息存放在session中
            session.setAttribute(ConstSession.USER_AVATAR_URL, user.getPotrait());
            return response;
        }
    }
    //Ajax检查用户输入的密码是否正确
    @ResponseBody
    @PostMapping(value = "/checkUserPwd")
    public Response checkUserPwd(@RequestBody IdCommand idCommand) {
        return userFacade.checkUserPwd(idCommand);
    }

    //更新用户密码

 /*   @ResponseBody
    @RequestMapping(value = "/goChangePwd.do", method = RequestMethod.GET)
    public ModelAndView goChangePwd() {
        return super.handleResponse(new Response(), "user/user-change-pwd");
    }*/

    //修改用户密码

    @ResponseBody
    @PostMapping(value = "/changePassword")
    public Response changePassword(@RequestBody IdCommand idCommand) {
        return userFacade.changeUserPwd(idCommand);
    }

    @ResponseBody
    @PostMapping(value = "/changeInfo")
    public Response changeInfo(@RequestBody ChaInfoCommand chaInfoCommand)
    {
        return  userFacade.changeUserInfo(chaInfoCommand);
    }

}
